<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar_box">
        <img src="./../../assets/logo.png" alt="">
      </div>
      <el-form class="login-form" :model="loginForm" :rules="loginFormRules" ref="ruleFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" size="large" placeholder="请输入用户名" :prefix-icon="UserFilled" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" size="large" type="password" show-password placeholder="请输入密码"
            :prefix-icon="Lock" />
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" size="large" @click="login(ruleFormRef)">登陆</el-button>
          <el-button type="info" size="large" @click="reset(ruleFormRef)">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { UserFilled, Lock } from '@element-plus/icons-vue'
import { ref, reactive, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus'
import { loginApi } from '@/api/interface'

const { proxy } = getCurrentInstance() as any
const router = useRouter()

interface Login {
  username: string,
  password: string
}

const loginForm = reactive<Login>({
  username: 'admin',
  password: '123456'
})
const ruleFormRef = ref<FormInstance>()
const loginFormRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
  ]
})

const login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async(valid, fields) => {
    if (valid) {
      const res = await loginApi({ username: loginForm.username, password: loginForm.password })

      if (res.meta.status === 200) {
        proxy.$message.success(res.meta.msg)
        window.sessionStorage.setItem('hmds-token', res.data.token)
        router.push('/home')
      } else {
        proxy.$message.error(res.meta.msg)
      }
    } else {
      console.log('error submit!', fields)
    }
  })

}

const reset = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>

<style lang="scss" scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
}

.login-box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}

.avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  width: 152px;
  height: 152px;

  img {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login-form {
  position: absolute;
  bottom: 20px;
  width: 100%;
  padding: 0 20px;
}

.login-btn {
  display: flex;
  justify-content: flex-end;
}
</style>